﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using App;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.LIB;
using YArchitech.ProgressBar;
using YArchitech.Revit.RevitExtension;
using HYCodeBase.TopologySuite;
using HYRevitCode.RevitTopologySuite;
using YJKRGeometry;
using Teigha.Core;
using Teigha.TD;
using TopologySearch;

namespace HYPretreatmentModeling
{
	internal class DWGDataManager : IDisposable
	{
		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		public DWGDataManager(ExternalCommandData cmdData)
		{
			this.m_cmdData = cmdData;
		}

		public void GetPolygonInnerValidObjs(string OdDbDataBasePath, ref List<LoopInfo> lstLoopsEdge)
		{
			try
			{
				int num = 0;
				int num2 = 100;
				YJKWindowHandle owner = new YJKWindowHandle(Process.GetCurrentProcess().MainWindowHandle);
				this.m_dlgProgress = new ProgressBarForm(num2, "正在读取图纸……", ProgressBarStyle.Blocks, true, false, false);
				this.m_dlgProgress.Show(owner);
				this.m_dlgProgress.RefreshProgressBar();
				num++;
				if (!this.m_dlgProgress.IsDisposed || !this.m_dlgProgress.m_stopTag)
				{
					OdDbObjectIdArray odDbObjectIdArray = this.SelectAllModelspaceObject(OdDbDataBasePath);
					this.m_dlgProgress.RefreshProgressBar();
					num++;
					if (!this.m_dlgProgress.IsDisposed || !this.m_dlgProgress.m_stopTag)
					{
						this.EraseProxyEntities(odDbObjectIdArray);
						if (odDbObjectIdArray.IsEmpty<OdDbObjectId>())
						{
							if (this.m_dlgProgress != null && !this.m_dlgProgress.IsDisposed)
							{
								this.m_dlgProgress.Dispose();
							}
						}
						else
						{
							this.m_dlgProgress.RefreshProgressBar();
							num++;
							if (!this.m_dlgProgress.IsDisposed || !this.m_dlgProgress.m_stopTag)
							{
								List<OdDbLine> lstLines = new List<OdDbLine>();
								List<OdDbBlockReference> list = new List<OdDbBlockReference>();
								List<List<OdDbLine>> list2 = new List<List<OdDbLine>>();
								OdGeMatrix3d kIdentity = OdGeMatrix3d.kIdentity;
								int num3 = 0;
								int count = odDbObjectIdArray.Count;
								int num4 = num2 - num;
								foreach (OdDbObjectId odDbObjectId in odDbObjectIdArray)
								{
									OdDbEntity odDbEntity = odDbObjectId.openObject(OpenMode.kForRead, false) as OdDbEntity;
									if (odDbEntity == null)
									{
										throw new Exception("读取图纸时发生错误。");
									}
									this.GroupEntity(odDbEntity, kIdentity, 0, ref lstLines, ref list, ref list2, ref this.m_lstTextInfo);
									int num5 = (int)Math.Ceiling((double)num3 / (double)count * (double)num4) - num;
									int num6 = 0;
									while (num6 < num5 && num < num2)
									{
										this.m_dlgProgress.RefreshProgressBar();
										num++;
										if (this.m_dlgProgress.IsDisposed && this.m_dlgProgress.m_stopTag)
										{
											return;
										}
										num6++;
									}
									num3++;
								}
								this.m_dlgProgress.Dispose();
								this.m_dlgProgress = new ProgressBarForm(2 + list.Count, "正在解析图纸……", ProgressBarStyle.Blocks, true, false, false);
								this.m_dlgProgress.Show(owner);
								List<LoopInfo> list3 = new List<LoopInfo>();
								foreach (List<OdDbLine> list4 in list2)
								{
									List<Curve> list5 = new List<Curve>();
									foreach (OdDbLine odDbLine in list4)
									{
										OdGePoint3d odGePoint3d = odDbLine.startPoint();
										OdGePoint3d odGePoint3d2 = odDbLine.endPoint();
										XYZ startPoint = new XYZ(odGePoint3d.x, odGePoint3d.y, 0.0);
										XYZ endPoint = new XYZ(odGePoint3d2.x, odGePoint3d2.y, 0.0);
										Curve item = YJKLineEx.YJKGetBound(startPoint, endPoint);
										list5.Add(item);
									}
									LoopInfo item2 = new LoopInfo(list5, 0.0001);
									list3.Add(item2);
								}
								this.m_dlgProgress.RefreshProgressBar();
								if (!this.m_dlgProgress.IsDisposed || !this.m_dlgProgress.m_stopTag)
								{
									List<List<Curve>> list6 = new List<List<Curve>>();
									if (this.SearchLoopByLines(lstLines, ref list6))
									{
										foreach (List<Curve> list7 in list6)
										{
											if (!list7.IsEmpty<Curve>())
											{
												LoopInfo item3 = new LoopInfo(list7, 0.0001);
												list3.Add(item3);
											}
										}
									}
									this.m_dlgProgress.RefreshProgressBar();
									if (!this.m_dlgProgress.IsDisposed || !this.m_dlgProgress.m_stopTag)
									{
										List<LoopInfo> collection = null;
										this.GetLoopsByBlocks(kIdentity, list, 0, out collection);
										list3.AddRange(collection);
										List<LoopInfo> collection2 = null;
										if (this.SearchSeparateLoops(list3, out collection2))
										{
											lstLoopsEdge.AddRange(collection2);
										}
										this.m_dlgProgress.Dispose();
									}
								}
							}
						}
					}
				}
			}
			catch (Exception)
			{
				if (this.m_dlgProgress != null && !this.m_dlgProgress.IsDisposed)
				{
					this.m_dlgProgress.Dispose();
				}
				throw;
			}
		}

		public List<ViewImpDWGInfo> ExportBlockFrameDatabase2Files(List<LoopInfo> lstLoopsEdge, string OdDbDataBasePath, out string AimSaveFilePath)
		{
			AimSaveFilePath = string.Empty;
			List<ViewImpDWGInfo> list = new List<ViewImpDWGInfo>();
			lstLoopsEdge.Sort(delegate(LoopInfo x, LoopInfo y)
			{
				double eps = DWGDataManager.m_dMinSheetEdgeLen * 0.05;
				if (x == null || y == null)
				{
					return 0;
				}
				if (Geometry.LessThan(x.RectMin.X, y.RectMin.X, eps))
				{
					return -1;
				}
				if (Geometry.IsEqual(x.RectMin.X, y.RectMin.X, eps) && Geometry.GreaterThan(x.RectMin.Y, y.RectMin.Y, eps))
				{
					return -1;
				}
				return 1;
			});
			List<FrameInfo> list2 = new List<FrameInfo>();
			if (!this.GetFrameInfo(ref list2, lstLoopsEdge))
			{
				return list;
			}
			List<OdDbObjectIdArray> list3 = new List<OdDbObjectIdArray>();
			ProgressBarForm progressBarForm = new ProgressBarForm(list2.Count);
			try
			{
				if (DWGDataManager.m_hWndRevit == null)
				{
					DWGDataManager.m_hWndRevit = new YJKWindowHandle(Process.GetCurrentProcess().MainWindowHandle);
				}
				progressBarForm.Show(DWGDataManager.m_hWndRevit);
				int wMsg = 7;
				DWGDataManager.SendMessage(DWGDataManager.m_hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				int num = OdDbDataBasePath.LastIndexOf('\\');
				string text = OdDbDataBasePath.Substring(num + 1);
				int startIndex = text.LastIndexOf('.');
				text = text.Remove(startIndex);
				this.SaveFilePath = string.Format(Product.UserDataLocation + "\\图纸预处理_{0}\\", text);
				if (Directory.Exists(this.SaveFilePath))
				{
					Directory.Delete(this.SaveFilePath, true);
					Directory.CreateDirectory(this.SaveFilePath);
				}
				else
				{
					Directory.CreateDirectory(this.SaveFilePath);
				}
				int num2 = 0;
				for (int i = 0; i < list2.Count; i++)
				{
					num2++;
					this.m_dbResult = null;
					ViewImpDWGInfo viewImpDWGInfo = new ViewImpDWGInfo();
					OdDbObjectIdArray odDbObjectIdArray = OdDbSelectionSet.select(this.m_db.activeViewportId(), list2[i].Pt3d, OdDbVisualSelection.Mode.kWPoly).objectIdArray();
					list3.Add(odDbObjectIdArray);
					this.m_db.getINSBASE();
					this.m_dbResult = this.m_db.wblock(odDbObjectIdArray, this.m_db.getINSBASE());
					this.m_strBlockFramePath = string.Concat(new object[]
					{
						this.SaveFilePath,
						"图纸",
						num2,
						".dwg"
					});
					this.m_dbResult.writeFile(this.m_strBlockFramePath, SaveType.kDwg, this.m_db.originalFileVersion(), false);
					viewImpDWGInfo.m_ArPoint3d = list2[i].Pt3d;
					viewImpDWGInfo.m_lstCurve = list2[i].lstCurve;
					viewImpDWGInfo.m_strDWGPath = this.m_strBlockFramePath;
					viewImpDWGInfo.m_strDWGName = list2[i].DrawingName;
					list.Add(viewImpDWGInfo);
					progressBarForm.setProgressBar();
				}
				progressBarForm.Close();
				progressBarForm.Dispose();
			}
			catch (Exception)
			{
				progressBarForm.Close();
				progressBarForm.Dispose();
			}
			AimSaveFilePath = this.SaveFilePath;
			this.m_dbResult = null;
			new ViewImpDWGInfo();
			OdDbObjectIdArray odDbObjectIdArray2 = new OdDbObjectIdArray();
			foreach (OdDbObjectIdArray odDbObjectIdArray3 in list3)
			{
				foreach (OdDbObjectId x2 in odDbObjectIdArray3)
				{
					odDbObjectIdArray2.Add(x2);
				}
			}
			this.m_dbResult = this.m_db.wblock(odDbObjectIdArray2, this.m_db.getINSBASE());
			this.m_dbResult.setLWDISPLAY(true);
			this.m_dbResult.getModelSpaceId().openObject(OpenMode.kForWrite);
			this.m_strBlockFramePath = this.SaveFilePath + "All.dwg";
			this.m_dbResult.writeFile(this.m_strBlockFramePath, SaveType.kDwg, this.m_db.originalFileVersion(), false);
			return list;
		}

		public void Dispose()
		{
			if (this.m_db != null)
			{
				this.m_db.Dispose();
				this.m_db = null;
			}
			if (this.m_dbResult != null)
			{
				this.m_dbResult.Dispose();
				this.m_dbResult = null;
			}
		}

		private void EraseProxyEntitiesByBlockRef(OdDbBlockReference odDbBlockRef)
		{
			OdDbObjectIterator odDbObjectIterator = ((OdDbBlockTableRecord)odDbBlockRef.blockTableRecord().openObject(OpenMode.kForWrite)).newIterator();
			odDbObjectIterator.start();
			while (!odDbObjectIterator.done())
			{
				OdDbEntity odDbEntity = odDbObjectIterator.entity(OpenMode.kForWrite, false);
				if (odDbEntity != null)
				{
					if (odDbEntity is OdDbProxyEntity)
					{
						OdDbProxyEntity odDbProxyEntity = odDbEntity as OdDbProxyEntity;
						if (!odDbProxyEntity.cloningAllowed() && odDbProxyEntity.eraseAllowed())
						{
							odDbEntity.erase(true);
						}
					}
					else if (odDbEntity is OdDbBlockReference)
					{
						this.EraseProxyEntitiesByBlockRef(odDbEntity as OdDbBlockReference);
					}
				}
				odDbObjectIterator.step();
			}
		}

		private void EraseProxyEntities(OdDbObjectIdArray idsInBoundary)
		{
			try
			{
				for (int i = 0; i < idsInBoundary.Count; i++)
				{
					OdDbEntity odDbEntity = idsInBoundary[i].safeOpenObject(OpenMode.kForWrite) as OdDbEntity;
					if (odDbEntity != null)
					{
						if (odDbEntity is OdDbProxyEntity)
						{
							OdDbProxyEntity odDbProxyEntity = odDbEntity as OdDbProxyEntity;
							if (!odDbProxyEntity.cloningAllowed() && odDbProxyEntity.eraseAllowed())
							{
								odDbEntity.erase(true);
								idsInBoundary.RemoveAt(i);
								i--;
							}
						}
						else if (odDbEntity is OdDbBlockReference)
						{
							this.EraseProxyEntitiesByBlockRef(odDbEntity as OdDbBlockReference);
						}
					}
				}
				OdDbSymbolTableIterator odDbSymbolTableIterator = (this.m_db.getLinetypeTableId().safeOpenObject(OpenMode.kForWrite) as OdDbLinetypeTable).newIterator();
				odDbSymbolTableIterator.start();
				while (!odDbSymbolTableIterator.done())
				{
					OdDbObjectId odDbObjectId = ((OdDbLinetypeTableRecord)odDbSymbolTableIterator.getRecordId().safeOpenObject()).extensionDictionary();
					if (!odDbObjectId.isNull())
					{
						OdDbDictionary odDbDictionary = odDbObjectId.safeOpenObject(OpenMode.kForWrite) as OdDbDictionary;
						if (odDbDictionary != null)
						{
							bool flag = false;
							OdDbDictionaryIterator odDbDictionaryIterator = odDbDictionary.newIterator();
							while (!odDbDictionaryIterator.done())
							{
								if (odDbDictionaryIterator.getObject() is OdDbProxyObject)
								{
									flag = true;
									break;
								}
								odDbDictionaryIterator.next();
							}
							if (flag)
							{
								odDbDictionary.erase(true);
							}
						}
					}
					odDbSymbolTableIterator.step();
				}
			}
			catch (Exception)
			{
				throw new Exception("未能清理代理对象，无法进行拆图");
			}
		}

		private OdDbObjectIdArray SelectAllModelspaceObject(string OdDbDataBasePath)
		{
			this.m_hostApp = TeighaServices.TeighaServicesInstance.HostAppServices;
			this.m_db = this.m_hostApp.readFile(OdDbDataBasePath);
			this.m_db.setCurrentUCS(OrthographicView.kTopView);
			this.m_db.activeViewportId().openObject(OpenMode.kForWrite);
			OdDbBlockTableRecord odDbBlockTableRecord = this.m_db.getModelSpaceId().safeOpenObject(OpenMode.kForWrite) as OdDbBlockTableRecord;
			OdGeExtents3d odGeExtents3d = new OdGeExtents3d();
			if (odDbBlockTableRecord.getGeomExtents(odGeExtents3d) != OdResult.eOk)
			{
				return new OdDbObjectIdArray();
			}
			OdGePoint3d[] array = new OdGePoint3d[4];
			double val = odGeExtents3d.maxPoint().x - odGeExtents3d.minPoint().x;
			double val2 = odGeExtents3d.maxPoint().y - odGeExtents3d.minPoint().y;
			double num = Math.Max(val, val2) * 0.01;
			array[0] = new OdGePoint3d(odGeExtents3d.minPoint().x - num, odGeExtents3d.minPoint().y - num, 0.0);
			array[1] = new OdGePoint3d(odGeExtents3d.minPoint().x - num, odGeExtents3d.maxPoint().y + num, 0.0);
			array[2] = new OdGePoint3d(odGeExtents3d.maxPoint().x + num, odGeExtents3d.maxPoint().y + num, 0.0);
			array[3] = new OdGePoint3d(odGeExtents3d.maxPoint().x + num, odGeExtents3d.minPoint().y - num, 0.0);
			return OdDbSelectionSet.select(this.m_db.activeViewportId(), array, OdDbVisualSelection.Mode.kCPoly).objectIdArray();
		}

		private bool GetFrameInfo(ref List<FrameInfo> lstFrameData, List<LoopInfo> lstLoopsEdge)
		{
			try
			{
				DrawingTextDistinguish drawingTextDistinguish = new DrawingTextDistinguish(this.m_lstTextInfo);
				Dictionary<string, int> dictionary = new Dictionary<string, int>();
				foreach (LoopInfo loopInfo in lstLoopsEdge)
				{
					int i = 0;
					int count = loopInfo.Curves.Count;
					if (count == 4)
					{
						FrameInfo frameInfo = new FrameInfo();
						string text = string.Empty;
						drawingTextDistinguish.GetDrawingNameInFrame(out text, loopInfo.RectMin, loopInfo.RectMax);
						if (string.IsNullOrEmpty(text))
						{
							text = "图纸";
						}
						if (dictionary.Count != 0 && dictionary.ContainsKey(text))
						{
							int num = dictionary[text];
							dictionary[text] = num + 1;
							text += num.ToString();
						}
						else
						{
							dictionary.Add(text, 1);
						}
						frameInfo.DrawingName = text;
						OdGePoint3d[] array = new OdGePoint3d[count];
						while (i < count)
						{
							Curve curve = loopInfo.Curves[i];
							Curve curve2;
							if (i == 0)
							{
								curve2 = loopInfo.Curves[count - 1];
							}
							else
							{
								curve2 = loopInfo.Curves[i - 1];
							}
							XYZ endPoint = curve.GetEndPoint(0);
							XYZ endPoint2 = curve.GetEndPoint(1);
							XYZ xyz = (endPoint - endPoint2).Normalize();
							XYZ xyz2 = (endPoint - curve2.GetEndPoint(0)).Normalize();
							XYZ xyz3 = endPoint + xyz * this.m_dSelectRangeExpand + xyz2 * this.m_dSelectRangeExpand;
							array[i] = new OdGePoint3d(xyz3.X, xyz3.Y, xyz3.Z);
							OdGePoint3d startPoint = new OdGePoint3d(endPoint.X, endPoint.Y, 0.0);
							OdGePoint3d endPoint3 = new OdGePoint3d(endPoint2.X, endPoint2.Y, 0.0);
							OdDbLine odDbLine = OdDbLine.createObject();
							odDbLine.setStartPoint(startPoint);
							odDbLine.setEndPoint(endPoint3);
							frameInfo.lstCurve.Add(odDbLine);
							i++;
						}
						frameInfo.Pt3d = array;
						frameInfo.ptBase = new OdGePoint3d(loopInfo.RectMin.X, loopInfo.RectMin.Y, 0.0);
						lstFrameData.Add(frameInfo);
					}
				}
			}
			catch (Exception)
			{
				return false;
			}
			return true;
		}

		private void GetLoopsByBlocks(OdGeMatrix3d mtrTrans, List<OdDbBlockReference> lstBlocks, int nDeepth, out List<LoopInfo> lstLoopByBlocks)
		{
			lstLoopByBlocks = new List<LoopInfo>();
			if (++nDeepth > this.m_nSearchBlockrefDeepth)
			{
				return;
			}
			for (int i = 0; i < lstBlocks.Count; i++)
			{
				OdDbBlockReference odDbBlockReference = lstBlocks[i];
				OdGeMatrix3d mtrTrans2 = odDbBlockReference.blockTransform().preMultBy(mtrTrans);
				if (!this.CheckBolck(odDbBlockReference, mtrTrans, lstLoopByBlocks))
				{
					List<OdDbLine> lstLines = null;
					List<OdDbBlockReference> lstBlocks2 = null;
					List<List<OdDbLine>> list = null;
					this.GetEntitisByBlock(odDbBlockReference, mtrTrans2, nDeepth, out lstLines, out lstBlocks2, out list);
					foreach (List<OdDbLine> list2 in list)
					{
						List<Curve> list3 = new List<Curve>();
						foreach (OdDbLine odDbLine in list2)
						{
							OdGePoint3d odGePoint3d = odDbLine.startPoint();
							OdGePoint3d odGePoint3d2 = odDbLine.endPoint();
							XYZ startPoint = new XYZ(odGePoint3d.x, odGePoint3d.y, 0.0);
							XYZ endPoint = new XYZ(odGePoint3d2.x, odGePoint3d2.y, 0.0);
							Curve item = YJKLineEx.YJKGetBound(startPoint, endPoint);
							list3.Add(item);
						}
						LoopInfo item2 = new LoopInfo(list3, 0.0001);
						lstLoopByBlocks.Add(item2);
					}
					List<List<Curve>> list4 = new List<List<Curve>>();
					if (this.SearchLoopByLines(lstLines, ref list4))
					{
						foreach (List<Curve> lstCurves in list4)
						{
							LoopInfo item3 = new LoopInfo(lstCurves, 0.0001);
							lstLoopByBlocks.Add(item3);
						}
					}
					List<LoopInfo> collection = null;
					this.GetLoopsByBlocks(mtrTrans2, lstBlocks2, nDeepth, out collection);
					lstLoopByBlocks.AddRange(collection);
					if (nDeepth == 1)
					{
						this.m_dlgProgress.RefreshProgressBar();
						if (this.m_dlgProgress.IsDisposed && this.m_dlgProgress.m_stopTag)
						{
							return;
						}
					}
				}
			}
		}

		private bool CheckBolck(OdDbBlockReference odDbBlock, OdGeMatrix3d mtrTrans, List<LoopInfo> lstRefLoops)
		{
			OdGeExtents3d odGeExtents3d = new OdGeExtents3d();
			if (odDbBlock.getGeomExtents(odGeExtents3d) != OdResult.eOk)
			{
				return false;
			}
			odGeExtents3d.transformBy(mtrTrans);
			XYZ xyz = new XYZ(odGeExtents3d.minPoint().x, odGeExtents3d.minPoint().y, 0.0);
			XYZ xyz2 = new XYZ(odGeExtents3d.maxPoint().x, odGeExtents3d.maxPoint().y, 0.0);
			double num = xyz2.X - xyz.X;
			double num2 = xyz2.Y - xyz.Y;
			if (num <= DWGDataManager.m_dMinSheetEdgeLen || num2 <= DWGDataManager.m_dMinSheetEdgeLen || num >= this.m_dMaxSheetEdgeLen || num2 >= this.m_dMaxSheetEdgeLen)
			{
				return false;
			}
			for (int i = 0; i < lstRefLoops.Count; i++)
			{
				LoopInfo loopInfo = lstRefLoops[i];
				if (this.IsRectBeIncluded(xyz, xyz2, loopInfo.RectMin, loopInfo.RectMax))
				{
					return true;
				}
			}
			return false;
		}

		private void GetEntitisByBlock(OdDbBlockReference oddbRef, OdGeMatrix3d mtrTrans, int nDeepth, out List<OdDbLine> lstLines, out List<OdDbBlockReference> lstBlocks, out List<List<OdDbLine>> lstPolylines)
		{
			lstLines = new List<OdDbLine>();
			lstBlocks = new List<OdDbBlockReference>();
			lstPolylines = new List<List<OdDbLine>>();
			try
			{
				OdDbObjectIterator odDbObjectIterator = ((OdDbBlockTableRecord)oddbRef.blockTableRecord().openObject(OpenMode.kForRead)).newIterator();
				odDbObjectIterator.start();
				while (!odDbObjectIterator.done())
				{
					OdDbEntity odDbEntity = odDbObjectIterator.entity(OpenMode.kForRead, false);
					if (odDbEntity != null)
					{
						this.GroupEntity(odDbEntity, mtrTrans, nDeepth, ref lstLines, ref lstBlocks, ref lstPolylines, ref this.m_lstTextInfo);
					}
					odDbObjectIterator.step();
				}
			}
			catch
			{
			}
		}

		private void GroupEntity(OdDbEntity entity, OdGeMatrix3d matrix, int nDepth, ref List<OdDbLine> lstLines, ref List<OdDbBlockReference> lstBlocks, ref List<List<OdDbLine>> lstPolylines, ref List<TextInfo> lstTextInfo)
		{
			if (entity.GetType() == typeof(OdDbBlockReference))
			{
				OdDbBlockReference item = OdDbBlockReference.cast(entity);
				lstBlocks.Add(item);
				return;
			}
			if (!(entity.GetType() == typeof(OdDbPolyline)))
			{
				if (entity.GetType() == typeof(OdDbLine))
				{
					OdDbLine odDbLine = (OdDbLine)OdDbLine.cast(entity).clone();
					odDbLine.transformBy(matrix);
					if (this.IsSheetEdge(odDbLine))
					{
						lstLines.Add(odDbLine);
						return;
					}
				}
				else
				{
					if (entity.GetType() == typeof(OdDbText))
					{
						TextInfo item2 = new TextInfo((OdDbText)OdDbText.cast(entity).clone(), nDepth, matrix);
						lstTextInfo.Add(item2);
						return;
					}
					if (entity.GetType() == typeof(OdDbMText))
					{
						TextInfo item3 = new TextInfo((OdDbMText)OdDbMText.cast(entity).clone(), nDepth, matrix);
						lstTextInfo.Add(item3);
					}
				}
				return;
			}
			OdDbPolyline odDbPolyline = OdDbPolyline.cast(entity);
			List<OdDbLine> list = new List<OdDbLine>();
			if (!this.CheckPolyline(out list, odDbPolyline, matrix))
			{
				return;
			}
			if (odDbPolyline.isClosed())
			{
				lstPolylines.Add(list);
				return;
			}
			lstLines.AddRange(list);
		}

		private bool CheckPolyline(out List<OdDbLine> lstPlyCrv, OdDbPolyline dbPl, OdGeMatrix3d blockTransform)
		{
			lstPlyCrv = new List<OdDbLine>();
			OdRxObjectPtrArray odRxObjectPtrArray = new OdRxObjectPtrArray();
			if (dbPl.subExplode(odRxObjectPtrArray) != OdResult.eOk)
			{
				return false;
			}
			foreach (OdRxObject pObj in odRxObjectPtrArray)
			{
				OdDbLine odDbLine = OdDbLine.cast(pObj);
				if (odDbLine == null)
				{
					return false;
				}
				OdDbLine odDbLine2 = (OdDbLine)odDbLine.clone();
				odDbLine2.transformBy(blockTransform);
				if (!this.IsSheetEdge(odDbLine2))
				{
					lstPlyCrv.Clear();
					return false;
				}
				lstPlyCrv.Add(odDbLine2);
			}
			return true;
		}

		private bool IsSheetEdge(OdDbLine oddbLine)
		{
			OdGePoint3d odGePoint3d = oddbLine.startPoint();
			OdGePoint3d odGePoint3d2 = oddbLine.endPoint();
			XYZ xyz = new XYZ(odGePoint3d.x, odGePoint3d.y, odGePoint3d.z);
			XYZ xyz2 = new XYZ(odGePoint3d2.x, odGePoint3d2.y, odGePoint3d2.z);
			XYZ a = (xyz2 - xyz).Normalize();
			if (!Geometry.IsParallel(a, XYZ.BasisX) && !Geometry.IsParallel(a, XYZ.BasisY))
			{
				return false;
			}
			double val = xyz.DistanceTo(xyz2);
			return !Geometry.Lessthan_Or_Equal(val, DWGDataManager.m_dMinSheetEdgeLen) && !Geometry.Greaterthan_Or_Equal(val, this.m_dMaxSheetEdgeLen);
		}

		private bool SearchSeparateLoops(List<LoopInfo> loops, out List<LoopInfo> lstSeparateLoops)
		{
			lstSeparateLoops = new List<LoopInfo>();
			List<LoopInfo> list = new List<LoopInfo>();
			list.AddRange(loops);
			for (int i = 0; i < list.Count; i++)
			{
				LoopInfo loopInfo = list[i];
				for (int j = 0; j < list.Count; j++)
				{
					if (j != i)
					{
						LoopInfo loopInfo2 = list[j];
						RevitPolygonDataBean inputOne = new RevitPolygonDataBean(CurveLoop.Create(loopInfo.Curves), null, 3);
						RevitPolygonDataBean inputTwo = new RevitPolygonDataBean(CurveLoop.Create(loopInfo2.Curves), null, 3);
						TopologyRelateKindEnum topologyRelateKindEnum;
						if (new RevitPolygonRelateOperater().TryGetPolygonRelate(inputOne, inputTwo, out topologyRelateKindEnum) && topologyRelateKindEnum != TopologyRelateKindEnum.Disjoint)
						{
							RevitPolygonBooleanOperater revitPolygonBooleanOperater = new RevitPolygonBooleanOperater();
							List<RevitPolygonDataBean> source = null;
							if (revitPolygonBooleanOperater.TryGetBooleanResult(inputOne, inputTwo, BooleanOperatorKindEnum.Intersection, out source) && !source.IsEmpty<RevitPolygonDataBean>())
							{
								if (source.Sum(delegate(RevitPolygonDataBean data)
								{
									if (data.Area == null)
									{
										return 0.0;
									}
									return data.Area.Value;
								}) >= 1.0)
								{
									double num = loopInfo.RectMax.X - loopInfo.RectMin.X;
									double num2 = loopInfo.RectMax.Y - loopInfo.RectMin.Y;
									double num3 = loopInfo2.RectMax.X - loopInfo2.RectMin.X;
									double num4 = loopInfo2.RectMax.Y - loopInfo2.RectMin.Y;
									double val = num * num2;
									double val2 = num3 * num4;
									if (Geometry.Lessthan_Or_Equal(val, val2))
									{
										list.RemoveAt(i);
										i--;
										break;
									}
								}
							}
						}
					}
				}
			}
			lstSeparateLoops.AddRange(list);
			return true;
		}

		private bool IsRectBeIncluded(XYZ ptRect1Min, XYZ ptRect1Max, XYZ ptRect2Min, XYZ ptRect2Max)
		{
			return Geometry.Greaterthan_Or_Equal(ptRect1Min.X, ptRect2Min.X) && Geometry.Greaterthan_Or_Equal(ptRect1Min.Y, ptRect2Min.Y) && Geometry.Lessthan_Or_Equal(ptRect1Max.X, ptRect2Max.X) && Geometry.Lessthan_Or_Equal(ptRect1Max.Y, ptRect2Max.Y);
		}

		private bool SearchLoopByLines(List<OdDbLine> lstLines, ref List<List<Curve>> loops)
		{
			List<Pole2D> list = new List<Pole2D>();
			int num = 0;
			int num2 = 400;
			foreach (OdDbCurve odDbCurve in lstLines)
			{
				if (!(odDbCurve.GetType() != typeof(OdDbLine)))
				{
					num++;
					if (num > num2)
					{
						break;
					}
					OdDbLine odDbLine = odDbCurve as OdDbLine;
					OdGePoint3d odGePoint3d = odDbLine.startPoint();
					OdGePoint3d odGePoint3d2 = odDbLine.endPoint();
					XYZ ptStart = new XYZ(odGePoint3d.x, odGePoint3d.y, 0.0);
					XYZ ptEnd = new XYZ(odGePoint3d2.x, odGePoint3d2.y, 0.0);
					Pole2D item = Pole2D.NewStructure2DPole(ElementId.InvalidElementId, ptStart, ptEnd, 0.0, 0.0);
					list.Add(item);
				}
			}
			new StructureLoopSearcher(this.m_cmdData).GetFullFloorEdgeLoops(list, ref loops);
			if (loops.IsEmpty<List<Curve>>())
			{
				return false;
			}
			for (int i = 0; i < loops.Count; i++)
			{
				if (loops[i].IsEmpty<Curve>())
				{
					loops.RemoveAt(i);
					i--;
				}
			}
			return true;
		}

		private static YJKWindowHandle m_hWndRevit = null;

		private List<OdDbCurve> m_lstNewCurve = new List<OdDbCurve>();

		private OdDbDatabase m_db;

		private OdDbDatabase m_dbResult;

		private string m_strBlockFramePath;

		private string SaveFilePath = Product.UserDataLocation + "\\图纸预处理\\";

		private ExternalCommandData m_cmdData;

		private ExHostAppServices m_hostApp;

		public static double m_dMinSheetEdgeLen = 14850.0;

		private double m_dMaxSheetEdgeLen = 237800.0;

		private int m_nMaxLayerLineCount = 5000;

		private List<LoopInfo> m_lstLoops = new List<LoopInfo>();

		private double m_dSelectRangeExpand = 2500.0;

		private int m_nSearchBlockrefDeepth = 2;

		private ProgressBarForm m_dlgProgress;

		private List<TextInfo> m_lstTextInfo = new List<TextInfo>();
	}
}
